<?php

namespace app\api\controller\haipi;

use app\common\controller\Frontend;
use app\common\model\haipi\Highend as HighendModel;
use app\common\model\haipi\HomeRecommendation;

class   Highend extends Frontend {
    protected array $noNeedLogin = ["*"];
    protected array $noNeedRight = ["*"];

    public function home(){
        $cityCode = $this->request->get('city_code');
        $where = [];
        $ids = HomeRecommendation::where('type', HomeRecommendation::TYPE_HIGHEND)->column('source_id');
        $list = HighendModel::field(['id', 'thumb_image', 'title'])
        ->order('weigh','desc')
        ->where($where)
        ->where(function($query) use($ids, $cityCode){
            if($cityCode){
                $query->where('city_code', $cityCode);
            }
            if(!empty($ids)){
                $query->whereIn('id', $ids);
            }
        })
        ->whereNotNull('contacts')
        ->limit(6)
        ->select();
        $this->success('', $list);
    }


    
    public function index(){
        $page = $this->request->get("page",1);
        $pagesize = $this->request->get("pagesize",20);
        $cityCode = $this->request->get("city_code");
        $districtCode = $this->request->get("district_code");
        if(empty($cityCode)){
            $this->error("缺少参数");
        }
        $showopage = $page == 1 ? true : false;
        $list = HighendModel::where('status', HighendModel::STATUS_NORMAL)
        ->where(function ($query) use($cityCode, $districtCode){      
                if(!empty($cityCode)){
                    $query->where('city_code', $cityCode);
                }
                if(!empty($districtCode)){
                    $query->where('district_code', $districtCode);  
                }
            })   
            ->field(['id', 'thumb_image', 'title', 'images', 'price', 'job', 'age', 'location','weight','height','education'])    
            ->order('weigh', 'desc')
            ->where('status', HighendModel::STATUS_NORMAL)
            ->paginate([
                'list_rows'=> $pagesize,
                'page' => $page,
            ], !$showopage);
            $data = [
                'items' => $list->items()
            ];
            if($showopage){
                $data['pageInfo']= [
                    'total' => $list->total(),
                    'totalPage' => $list->lastPage(),
                    'currentPage' => $page,
                ];
            }
        $this->success('', $data);
    }


    public function read(){
        $id = $this->request->get('id');
        if(!$id){
            $this->error("缺少参数");
        }
        $data = HighendModel::field(['id', 'thumb_image', 'title', 
        'images', 'age','job','intro','education',
        'location', 'price', 'weight','height',
        'video'
        ])->where("id", $id)->find();
        $this->success('',$data);
     }
}